import shutil
import os
import stat
import time
import exifread as exif
import re
import sys
import hashlib
import datetime

class ReadFailException(Exception):
    pass

def filt_img(dir):
    img_type=('.jpg','.PNG','.png','.JPG')
    img_list=[]
    for root,dirs,files in os.walk(dir):
        for file in files:
            if (os.path.splitext(file)[1] in img_type):
                # print os.path.join(root,file)
                img_list.append(os.path.join(root,file).replace('\\','/'))
    return img_list

def getDate(filename):
    try:
        fd = open(filename, 'rb')
    except:
        raise ReadFailException,"unopen file[%s]\n" % filename
    data = exif.process_file( fd )
    if data:
        try:
            t = data['EXIF DateTimeOriginal']
            return str(t).replace(":","-")[:10]#token time
        except:
            pass
    state = os.stat(filename)
    return time.strftime("%Y-%m-%d", time.localtime(state[-2]))#create time

def get_file_hash(filename):
    sig=hashlib.sha256(open(filename,'rb').read()).hexdigest()
    return sig

def Current_Date():
    tmp=time.localtime(time.time())
    ans=time.strftime('%Y-%m-%d',time.localtime(time.time()))
    return ans

def Current_DateTime():
    tmp=time.localtime(time.time())
    ans=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    return ans

def get_time_stamp():
    tamp = time.time()
    result=int(tamp*1000)
    return result

def is_valid_date(str):
    try:
        time.strptime(str,"%Y-%m-%d")
        return True
    except:
        return False

def count_date(dstr,cnt):
    if is_valid_date(dstr):
        tmp=datetime.datetime.strptime(dstr, "%Y-%m-%d").date()
        newdate=tmp + datetime.timedelta(cnt)
        ans=str(newdate)
        return ans
    else:
        return str(datetime.date.today())



# r=Current_DateTime()
# print r
# print type(r)
# aaa=get_time_stamp()
# print aaa
# type(aaa)

# pt='F:/style-transfer/style-transfer-master/images/content/minjiang.jpg'
# r=getDate(pt)
# print r
# print type(r)
# r=get_file_hash(pt)
# print r
# print type(r)
# print 'Len: ',len(r)

# pp='F:/face-recognition/age_gender'
# fs=filt_img(pp)
# for i in fs:
#     print i

# pt='F:/test_jpg/fy.jpg'
# r=getDate(pt)
# print r

# try:
#     photo='F:/code/pycaffe/photo_datetime/mq.jpg'
#     f = open(photo, 'rb')
# except:
#     raise ReadFailException, "unopen file[%s]\n" % photo

# tags = EXIF.process_file(f)
# chtime = str(tags['EXIF DateTimeOriginal'])
# print 'The datetime of the photo %s taken is %s' % (photo,chtime)

#How to use:
# dir="F:/code/py/other/files_filter/classify_content"
# lst=filt_img(dir)
# for item in lst:
#     tt=getDate(item)
#     print item,'   ',tt
#     # print type(item)

# F:/code/py/other/files_filter/classify_content\1.PNG     2017-03-22
# F:/code/py/other/files_filter/classify_content\1\minjiang.jpg     2017-02-05 13-55-52
# F:/code/py/other/files_filter/classify_content\1\mq.jpg     2017-01-27 16-57-55
# F:/code/py/other/files_filter/classify_content\one\1.PNG     2017-03-22
# F:/code/py/other/files_filter/classify_content\one\2.PNG     2017-03-22
# F:/code/py/other/files_filter/classify_content\one\3.PNG     2017-03-22
# F:/code/py/other/files_filter/classify_content\qwe\squirrel.jpg     2017-03-03
# F:/code/py/other/files_filter/classify_content\qwe\xiongjiang.jpg     2017-02-05 14-37-33
# F:/code/py/other/files_filter/classify_content\qwe\two\huangtulin.jpg     2017-02-05 14-30-23
# F:/code/py/other/files_filter/classify_content\qwe\two\ert\1.PNG     2017-03-22
# F:/code/py/other/files_filter/classify_content\two\huangtulin.jpg     2017-02-05 14-30-23